package com.dog.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.framework.mybatis.utils.PageUtils;
import com.dog.modules.system.convert.SysDictDataConvert;
import com.dog.modules.system.domain.SysDictData;
import com.dog.modules.system.api.query.SysDictDataQuery;
import com.dog.modules.system.api.vo.SysDictDataVO;
import com.dog.modules.system.mapper.SysDictDataMapper;
import com.dog.modules.system.service.SysDictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sys_dict_data(字典数据)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Override
    public PageResult<SysDictDataVO> page(SysDictDataQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<SysDictData> sysDictDataList = this.list(getWrapper(query));
        PageInfo<SysDictData> pageInfo = new PageInfo<>(sysDictDataList);
        List<SysDictDataVO> sysDictDataVOList = SysDictDataConvert.INSTANCE.convertList(pageInfo.getList());
        return PageUtils.getPage(pageInfo, sysDictDataVOList);
    }

    private Wrapper<SysDictData> getWrapper(SysDictDataQuery query){
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictData::getDictTypeId, query.getDictTypeId());
        wrapper.orderByAsc(SysDictData::getSort);
        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDataVO vo) {
        SysDictData entity = SysDictDataConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDataVO vo) {
        SysDictData entity = SysDictDataConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }
}




